package com.css.dzj.dzjap.modules.record.community.contorller;

import com.css.dzj.dzjap.common.annotation.SysLog;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.record.community.entity.ApRecordGainEntity;
import com.css.dzj.dzjap.modules.record.community.service.CommunityRecordService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 地震小区划工作成果备案信息表增删改查
 *
 * @author zhouchanghe
 */
@RestController
@RequestMapping("/recordgain")
public class CommunityRecordController {

    @Autowired
    private CommunityRecordService communityRecordService;


    @GetMapping("/findPage")
    public R getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                     @RequestParam(name = "userName", required = false) String userName,
                     @RequestParam(name = "recordProjectName", required = false) String recordProjectName,
                     @RequestParam(name = "recordUser", required = false) String recordUser,
                     @RequestParam(name = "status", required = false) String status,
                     @RequestParam(name = "recordPersonName", required = false) String recordPersonName,
                     @RequestParam(name = "recordTimeStart", required = false) String recordTimeStart,
                     @RequestParam(name = "recordTimeEnd", required = false) String recordTimeEnd,
                     @RequestParam(name = "recordType", required = false) String recordType,
                     @RequestParam(name = "recordProjectAddress", required = false) String recordProjectAddress,
                     @RequestParam(name = "reportStatus", required = false) String reportStatus
    ) {
        if (StringUtils.isEmpty(recordProjectAddress)) {
            return R.error("该用户没有权限");
        }
        //地震小区划工作成果备案信息
        recordType = Constant.EARTHQU_SUBDIVI;
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        QueryResults<ApRecordGainEntity> page = communityRecordService.getPage(userName,recordProjectName, status, recordUser, recordTimeStart, recordTimeEnd, recordType, recordProjectAddress, reportStatus, pageRequest);
        return R.ok().put("data", page);
    }
    @SysLog("获取地震小区划工作成果备案详情信息")
    @PostMapping("/getRecordgain")
    public R RecordGainById(@RequestParam(name = "id") String id) {
            ApRecordGainEntity apRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            return R.ok().put("data",apRecordGainEntity);
    }

    /**
     * 地震小区划工作成果备案添加
     *
     * @param apRecordGainEntity
     * @return
     */
    @SysLog("创建地震小区划工作成果备案")
    @PostMapping("/add")
    public R add(@RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {
            //备案类型： 0.区域地震安全性评估成果1.地震小区划工作成果
            apRecordGainEntity.setRecordType(Constant.EARTHQU_SUBDIVI);
            communityRecordService.save(apRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过id删除地震小区划工作成果备案
     *
     * @param id
     * @return
     */
    @SysLog("逻辑删除地震小区划工作成果备案")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        try {
            ApRecordGainEntity apRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            if (apRecordGainEntity == null) {
                return R.error("地震小区划工作成果备案信息不存在");
            }
            apRecordGainEntity.setDelFlag(Constant.DELETE);
            communityRecordService.update(apRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @SysLog("修改地震小区划工作成果备案")
    @PostMapping("/update/{id}")
    public R update(@PathVariable String id, @RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {
            ApRecordGainEntity targetApRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            BeanUtil.copyBeanNotNullToBean(apRecordGainEntity, targetApRecordGainEntity);
            targetApRecordGainEntity.setUpdateTime(new Date());
            communityRecordService.update(targetApRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 地震小区划工作成果备案暂存
     *
     * @param apRecordGainEntity * @return
     */
    @SysLog("暂存地震小区划工作成果备案")
    @PostMapping("/zcadd")
    public R zcadd(@RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {
            //备案类型： 0.区域地震安全性评估成果1.地震小区划工作成果
            apRecordGainEntity.setRecordType(Constant.EARTHQU_SUBDIVI);
            communityRecordService.zcsave(apRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @SysLog("审批地震小区划工作成果备案")
    @PostMapping("/check/{id}")
    public R check(@PathVariable String id, @RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {
            ApRecordGainEntity targetApRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            BeanUtil.copyBeanNotNullToBean(apRecordGainEntity, targetApRecordGainEntity);
            //审核结果不通过
            if (apRecordGainEntity.getCheckResult().equals(Constant.CHECK_RESULT_NO)) {
                targetApRecordGainEntity.setStatus(Constant.CHECK_STATUS2);
            }
            //审核结果通过
            if (apRecordGainEntity.getCheckResult().equals(Constant.CHECK_RESULT_YES)) {
                targetApRecordGainEntity.setStatus(Constant.CHECK_STATUS3);
            }
            targetApRecordGainEntity.setUpdateTime(new Date());
            communityRecordService.check(targetApRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @SysLog("撤回地震小区划工作成果备案")
    @PostMapping("/cancel/{id}")
    public R cancel(@PathVariable String id, @RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {

            ApRecordGainEntity targetApRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            //未提交（已撤回）
            targetApRecordGainEntity.setStatus(Constant.CHECK_STATUS0);
            targetApRecordGainEntity.setUpdateTime(new Date());
            communityRecordService.cancel(targetApRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @SysLog("上报地震小区划工作成果备案")
    @PostMapping("/report/{id}")
    public R report(@PathVariable String id, @RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {

            ApRecordGainEntity targetApRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            //已上报
            targetApRecordGainEntity.setReportStatus(Constant.REPORT_STATUS_YES);
            targetApRecordGainEntity.setUpdateTime(new Date());
            communityRecordService.update(targetApRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @SysLog("提交地震小区划工作成果备案")
    @PostMapping("/submit/{id}")
    public R submit(@PathVariable String id, @RequestBody ApRecordGainEntity apRecordGainEntity) {
        try {

            ApRecordGainEntity targetApRecordGainEntity = communityRecordService.queryApRecordGainById(id);
            BeanUtil.copyBeanNotNullToBean(apRecordGainEntity, targetApRecordGainEntity);
            //已提交待审批
            targetApRecordGainEntity.setStatus(Constant.CHECK_STATUS1);
            targetApRecordGainEntity.setUpdateTime(new Date());
            communityRecordService.submit(targetApRecordGainEntity);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
    @SysLog("地震小区划工作添加通知公告")
    @PostMapping("/addHandle")
    public R addHandle(@RequestBody ApPublicEntity pub){
        try {
            //备案类型： 0.区域地震安全性评估成果1.地震小区划工作成果
            String RecordType=Constant.EARTHQU_SUBDIVI;
            communityRecordService.addHandle(pub,RecordType);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }
    /**
     * 获取项目备案表数据信息
     */
    @GetMapping("/queryAllRecordGain")
    public List queryAllRecordGain() {
        String s = "";
        try {
            List<ApRecordGainEntity> list = communityRecordService.queryAllRecordGain();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
